package ltd.cheers.mall.service.impl;

import ltd.cheers.mall.common.ServiceResultEnum;
import ltd.cheers.mall.controller.vo.CheersMallSearchGoodsVO;
import ltd.cheers.mall.dao.CheersMallGoodsMapper;
import ltd.cheers.mall.entity.CheersMallGoods;
import ltd.cheers.mall.service.CheersMallGoodsService;
import ltd.cheers.mall.util.BeanUtil;
import ltd.cheers.mall.util.PageQueryUtil;
import ltd.cheers.mall.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: LiHuanYang
 * @date: 2020/3/24 下午 09:45
 */
@Service
public class CheersMallGoodsServiceImpl implements CheersMallGoodsService {

    @Autowired
    private CheersMallGoodsMapper goodsMapper;

    @Override
    public PageResult getCheersMallGoodsPage(PageQueryUtil pageUtil) {
        //当前页码中的数据列表
        List<CheersMallGoods> goodsList = goodsMapper.findCheersMallGoodsList(pageUtil);
        //数据总条数 用于计算分页数据
        int total = goodsMapper.getTotalCheersMallGoods(pageUtil);
        //分页信息封装
        PageResult pageResult = new PageResult(goodsList, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public String saveCheersMallGoods(CheersMallGoods goods) {
        if (goodsMapper.insertSelective(goods) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public void batchSaveCheersMallGoods(List<CheersMallGoods> cheersMallGoodsList) {
        if (!CollectionUtils.isEmpty(cheersMallGoodsList)) {
            goodsMapper.batchInsert(cheersMallGoodsList);
        }
    }

    @Override
    public String updateCheersMallGoods(CheersMallGoods goods) {
        CheersMallGoods temp = goodsMapper.selectByPrimaryKey(goods.getGoodsId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        goods.setUpdateTime(new Date());
        if (goodsMapper.updateByPrimaryKeySelective(goods) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public CheersMallGoods getCheersMallGoodsById(Long id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean batchUpdateSellStatus(Long[] ids, int sellStatus) {
        return goodsMapper.batchUpdateSellStatus(ids, sellStatus) > 0;
    }

    @Override
    public PageResult searchCheersMallGoods(PageQueryUtil pageUtil) {
        List<CheersMallGoods> goodsList = goodsMapper.findCheersMallGoodsListBySearch(pageUtil);
        int total = goodsMapper.getTotalCheersMallGoodsBySearch(pageUtil);
        List<CheersMallSearchGoodsVO> cheersMallSearchGoodsVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(goodsList)) {
            cheersMallSearchGoodsVOS = BeanUtil.copyList(goodsList, CheersMallSearchGoodsVO.class);
            for (CheersMallSearchGoodsVO cheersMallSearchGoodsVO : cheersMallSearchGoodsVOS) {
                String goodsName = cheersMallSearchGoodsVO.getGoodsName();
                String goodsIntro = cheersMallSearchGoodsVO.getGoodsIntro();
                // 字符串过长导致文字超出的问题
                if (goodsName.length() > 28) {
                    goodsName = goodsName.substring(0, 28) + "...";
                    cheersMallSearchGoodsVO.setGoodsName(goodsName);
                }
                if (goodsIntro.length() > 30) {
                    goodsIntro = goodsIntro.substring(0, 30) + "...";
                    cheersMallSearchGoodsVO.setGoodsIntro(goodsIntro);
                }
            }
        }
        PageResult pageResult = new PageResult(cheersMallSearchGoodsVOS, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }


}
